package com.turing.post.cardmanagement.impl;

import java.math.BigInteger;
import java.util.List;

import javax.ejb.Local;
import javax.ejb.Remote;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import com.turing.post.cardmanagement.util.SalesGetCardOrderDetail;
import com.turing.post.cardmanagement.util.SalesGetCardOrderDetailDao;
import com.turing.post.util.GeneralDao;
import com.turing.post.util.PCPSApplicationException;

/**
 * 临时领卡\缴回单明细dao接口实现
 * 
 * @author kangzhe
 * 
 */
@Stateless(mappedName = "cardmanagement.impl.SalesGetCardOrderDetailDaoImpl")
@Remote
@Local
public class SalesGetCardOrderDetailDaoImpl implements
		SalesGetCardOrderDetailDao {
	/**
	 * 实体管理器
	 * 
	 * @see javax.persistence.EntityManager
	 */
	@PersistenceContext
	protected EntityManager _em;

	@Override
	public void add(SalesGetCardOrderDetail sgcod)
			throws PCPSApplicationException {
		GeneralDao.insert(_em, sgcod);
	}

	@Override
	public SalesGetCardOrderDetail getInfoByDetailId(
			BigInteger salesGetCardOrderId, Integer orderItemSeqNo) {
		String sql = "select * from salesgetcardorderdetail t "
				+ "where t.salesgetcardorderid=?1 and t.orderitemseqno=?2";
		Query query = _em.createNativeQuery(sql, SalesGetCardOrderDetail.class);
		query.setParameter(1, salesGetCardOrderId);
		query.setParameter(2, orderItemSeqNo);
		return (SalesGetCardOrderDetail) query.getSingleResult();
	}

	@Override
	public void update(SalesGetCardOrderDetail sgcod)
			throws PCPSApplicationException {
		GeneralDao.update(_em, sgcod);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<SalesGetCardOrderDetail> getSalesGetCardOrderDetailList(
			BigInteger salesGetCardOrderId) {
		String sql = "select * from salesgetcardorderdetail t where t.salesgetcardorderid=?1";
		Query query = _em.createNativeQuery(sql, SalesGetCardOrderDetail.class);
		query.setParameter(1, salesGetCardOrderId);
		return query.getResultList();
	}

}
